﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GWModflow.FileLayers
{
    public class PBlockCenteredFlow:Package
    {

        public PBlockCenteredFlow(int layerNum)
        {
            this._fileType = FileType.BCF6;

            this.Nunit = 22;
            this.Fname = "solution.bc6";

            this._Ltype=new int[layerNum];
            this._TRPY=new double[layerNum];
            this.LAYCON = new int[layerNum];

        }

        int _IBCFCB = 109;

        /// <summary>
        /// 是否输出cell by cell term ,<0 输出到指定的UNIT >0 输出到lst文件
        /// </summary>
        public int IBCFCB
        {
            get { return _IBCFCB; }
            set { _IBCFCB = value; }
        }


        double _HDRY=1.0;

        /// <summary>
        /// 单元水头在模拟中标记为干。尽管这个值在模拟中没有用处，
        /// 他在寻找模型输出的结果水头仍是有用的。
        /// 他与基本包中的HNOFLO基本相似
        /// </summary>
        public double HDRY
        {
            get { return _HDRY; }
            set { _HDRY = value; }
        }


        int _IWDFLG=0;


        /// <summary>
        /// 是否启用湿化功能 0不开启
        /// </summary>
        public int IWDFLG
        {
            get { return _IWDFLG; }
            set { _IWDFLG = value; }
        }

        double _WETFCT=0;

        /// <summary>
        /// 湿化因子
        /// </summary>
        public double WETFCT
        {
            get { return _WETFCT; }
            set { _WETFCT = value; }
        }


        int _IWETIT=1;

        /// <summary>
        /// 湿化间隔
        /// </summary>
        public int IWETIT
        {
            get { return _IWETIT; }
            set { _IWETIT = value; }
        }

        int _IHDWET=0;

        /// <summary>
        /// 湿化过程中， 初始水头的计算公式 0 or not 0
        /// </summary>
        public int IHDWET
        {
            get { return _IHDWET; }
            set { _IHDWET = value; }
        }



        int[] _Ltype;

    
       /// <summary>
        /// 层类型设置与NLAY字体组合起来使用比如：2层第3个类型，设置数值为23
       /// </summary>
        public int[] Ltype
        {
            get { return _Ltype; }
            set { _Ltype = value; }
        }

        int[] _LAYCON;
        /// <summary>
        ///  层类型设置
        /// </summary>
        public int[] LAYCON
        {
            get { return _LAYCON; }
            set { _LAYCON = value; }
        }       
        double[] _TRPY;
      

        /// <summary>
        /// 一维数据，所有图层共用的因子
        /// </summary>
        public double[] TRPY
        {
            get { return _TRPY; }
            set { _TRPY = value; }
        }


        List<double[,]> _Sf1=new List<double[,]>();

        /// <summary>
        /// 当至少存在一个tansient层时，需要此数据
        /// </summary>
        public List<double[,]> Sf1
        {
            get { return _Sf1; }
            set { _Sf1 = value; }
        }


        List<double[,]> _Tran=new List<double[,]>();



        /// <summary>
        /// 
        /// 只在层类型设置为 0 或者2 时输出此数据，行的投射比，Tran乘以TRPY等于列的透射比
        /// </summary>
        public List<double[,]> Tran
        {
            get { return _Tran; }
            set { _Tran = value; }
        }


        List<double[,]> _HY=new List<double[,]>();

        /// <summary>
        /// 当层类型为1或者3时，需要此数据，行的水力传导系数。HY乘以TRPY等于列的水力传导系数
        /// </summary>
        public List<double[,]> HY
        {
            get { return _HY; }
            set { _HY = value; }
        }


       List<double[,]> _Vcont=new List<double[,]>();

        /// <summary>
        /// 非最后一层时，需要输出此，数据，是否多层？
        /// </summary>
       public List<double[,]> Vcont
        {
            get { return _Vcont; }
            set { _Vcont = value; }
        }

       List<double[,]> _Sf2=new List<double[,]>();

        /// <summary>
        /// 至少存在一个瞬时周期，且层类型为2或者3
        /// </summary>
       public List<double[,]> Sf2
       {
           get { return _Sf2; }
           set { _Sf2 = value; }
       }


       List<double[,]> _WETDRY=new List<double[,]>();

        /// <summary>
        /// 层类型为1或者3，且IWDFLG不为0，输出此数据
        /// </summary>
       public List<double[,]> WETDRY
       {
           get { return _WETDRY; }
           set { _WETDRY = value; }
       }

       private bool _IsHaveTransient = false;
        /// <summary>
       /// 是否存在时间离散，默认否
        /// </summary>
       public bool IsHaveTransient
       {
           get { return _IsHaveTransient; }
           set { _IsHaveTransient = value; }
       }
       



        public override void WriteFile(string path)
        {
            string content = "";

            //content = "# HuanAnKeJi BCF File" + "\r\n";
            content += this.IBCFCB + "  " + this.HDRY + "  " + this.IWDFLG + "  " + this.WETFCT + "  " + this.IWETIT + "  " + this.IHDWET + "            Item 1: IBCFCB HDRY IWDFLG WETFCT IWETIT IHDWET" + "\r\n";
            for (int i = 0; i < this.Ltype.Length; i++)
            {
                content += this.LAYCON[i].ToString() + this.Ltype[i].ToString() + "  ";
            }
            content += "                    Item 2: Ltype" + "\r\n";

            //Item 3:  TRPY
            if (IsConstant(this.TRPY))
            {
                content += "CONSTANT " + string.Format("{0,10}", this.TRPY[0]) + "             Item 3:  TRPY" + "\r\n";
            }
            else
            {
                content += "INTERNAL   1.0 (FREE)  1" + "             Item 3:  TRPY" + "\r\n";
                string str = "";                
                if (GetArrayString(this.TRPY, ref str))
                {
                    content += str;
                }
            }



            for (int i = 0; i < this.Sf1.Count; i++)
            {
                //Item 4: S, layer 1 (Specific Yield)
                if (this.IsHaveTransient)
                {
                    if (IsConstant(this.Sf1[i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.Sf1[i][0, 0]) + "             Item 4: S, layer "+(i+1).ToString ()+" (Specific Yield)" + "\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "             Item 4: S, layer " + (i + 1).ToString() + " (Specific Yield)" + "\r\n";
                        string str = "";
                        if (GetArrayString(this.Sf1[i], ref str))
                        {
                            content += str;
                        }
                    }
                }

                //Item 5: T, layer 2              
                if (this.Ltype[i] == 0 || this.Ltype[i] == 2)
                {
                    if (IsConstant(this.Tran[i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.Tran[0][0, 0]) + "             Item 5: T, layer "+(i+1).ToString ()+ "\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "             Item 5: T, layer "+(i+1).ToString ()+  "\r\n";
                        string str = "";
                        if (GetArrayString(this.Tran[i], ref str))
                        {
                            content += str;
                        }
                    }
                }

                //Item 6: K, layer 1
                if (this.Ltype[i] == 1 || this.Ltype[i] == 3)
                {
                    if (IsConstant(this.HY[i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.HY[0][0, 0]) + "             Item 6: K, layer "+(i+1).ToString ()+"\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "             Item 6: K, layer " + (i + 1).ToString()  +"\r\n";
                        string str = "";
                        if (GetArrayString(this.HY[i], ref str))
                        {
                            content += str;
                        }
                    }
                }


                //Item 7: VCONT, layer 1  不写最底层数据
                if (this.Vcont.Count > 1 && i < this.Vcont.Count - 1)
                {
                    if (IsConstant(this.Vcont[i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.Vcont[0][0, 0]) + "             Item 7: VCONT, layer " + (i + 1).ToString() + "\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "             Item 7: VCONT, layer" + (i + 1).ToString() + "\r\n";
                        string str = "";
                        if (GetArrayString(this.Vcont[i], ref str))
                        {
                            content += str;
                        }
                    }
                }

                // Item 8: VHY/THICK-2
                if (this.IsHaveTransient)
                {
                    if (this.Ltype[i] == 2 || this.Ltype[i] == 3)
                    {
                        if (IsConstant(this.Sf2[i]))
                        {
                            content += "CONSTANT " + string.Format("{0,10}", this.Sf2[0][0, 0]) + "              Item 8: VHY/THICK-" + (i + 1).ToString() + "\r\n";
                        }
                        else
                        {
                            content += "INTERNAL   1.0 (FREE)  19" + "              Item 8: VHY/THICK-" + (i + 1).ToString() + "\r\n";
                            string str = "";
                            if (GetArrayString(this.Sf2[i], ref str))
                            {
                                content += str;
                            }
                        }
                    }
                }


                if ((this.IWDFLG != 0 && this.Ltype[i] == 1) || (this.IWDFLG != 0 && this.Ltype[i] == 3))
                {
                    if (IsConstant(this.WETDRY[i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.WETDRY[0][0, 0]) + "              Item 9: ---" + (i + 1).ToString()  +"\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "              Item 9: ---" + (i + 1).ToString() + "\r\n";
                        string str = "";
                        if (GetArrayString(this.WETDRY[i], ref str))
                        {
                            content += str;
                        }
                    }
                }
            }              
            string fname = path + this.Fname;
            FXnaInfrastructure.FileHelper.WriteFile(fname, content);
        }


    }
}
